/*
 * File:   NewForm.cpp
 * Author: lynn
 *
 * Created on 2010年11月1日, 下午4:11
 */

#include "NewForm.h"
#include <QFileSystemModel>
#include <QMessageBox>
#include"secondForm.h"
#include<QRegExp>
#include "Data.h"
#include "Locate.h"
#include "Track.h"
#include "Control.h"
#include "PreProcess.h"

extern int numFrame;

//启动avi文件读取模块
void StartAvi(char* path, NewForm *form)
{
    Camera cam0;

    //图像预处理类
    PreProcess myPrePro0(&cam0);
    //目标检测类
    Locate myLoc0(&cam0);
    //目标跟踪类
    Track myTrack0(&cam0);
    //控制类
    Control myCon;

    if(myCon.OpenAvi(cam0, path) == true){
        double time;
        clock_t start, end;
        secondForm *sForm = new secondForm(cam0.pFrame, form);
        sForm->show();

        do{
            start = clock();

            //判断是否获取成功
            if(!cam0.pFrame){
                cout<<"图像获取失败"<<endl;
                return ;
            }
//            if(numFrame == 52){
//                cvShowImage("bk", cam0.pBkImg);
//            }
            myPrePro0.Process(cam0.pFrame, &cam0);
            ///////////////调试代码//////////////////////////////////
            cvConvert(cam0.pBkMat, cam0.pBkImg);
            //cvConvert(pFrMat, pFrImg);
            //cvConvert(pGrayMat, pGray);

            
            
            if(numFrame > 51)
            {
                myLoc0.scan(cam0.pFrMat);
                myLoc0.Draw(cam0.pFrImg, CV_RGB(255,255,255));
//                cvWaitKey(0);

                if(numFrame == 52)
                    myTrack0.Init();

                myTrack0.Run(myLoc0.V);
            }

            cvCopy(cam0.pFrame, cam0.pPre);

            if(numFrame > 51)
            {
                myTrack0.Draw(cam0.pFrame, CV_RGB(255,0,0));
                //cvShowImage("DetectorVPA1.0", cam0.pFrame);
                sForm->nextFrame(cam0.pFrame);
            }

            cvShowImage("bk", cam0.pBkImg);
            cvShowImage("fr", cam0.pFrImg);

            ///////////////////////////////////////////////////////

            end = clock();
            time = (double)(end - start)/CLOCKS_PER_SEC * 1000;
            cout<<"time:"<<time<<endl;
            time = 60 - time;
            if(time <= 0) time = 1;
            cvWaitKey(10);
        }
        while(cam0.pFrame = cvQueryFrame(cam0.capture));
    }
}

NewForm::NewForm(QWidget *parent):QDialog(parent) {
    setupUi(this);
    QRegExp rge("[1-9]{0,3}");
    this->leHight->setValidator(new QRegExpValidator(rge,this));
    this->leSpace->setValidator(new QRegExpValidator(rge,this));
    this->showTreeView();
    fileName="";
    connect(leHight,SIGNAL(textChanged(QString)),this,SLOT(showMessage(QString)));
    connect(leSpace,SIGNAL(textChanged(QString)),this,SLOT(showMsg(QString)));
    connect(this->btnCamera,SIGNAL(clicked()),this,SLOT(showSecondFormC()));
    connect(this->btnRecord,SIGNAL(clicked()),this,SLOT(showSecondFormR()));

}

//NewForm::~NewForm() {
//}
int NewForm::showTreeView()
{
    model = new QDirModel;
    model->setReadOnly(false);
    model->setSorting(QDir::DirsFirst | QDir::IgnoreCase | QDir::Name);
    this->treeView->setModel(model);
    this->treeView->setRootIndex(model->index(QDir::currentPath()));
    this->treeView->header()->setStretchLastSection(true);
    this->treeView->header()->setSortIndicator(0,Qt::AscendingOrder);
    this->treeView->header()->setSortIndicatorShown(true);
    this->treeView->header()->setClickable(true);
    QModelIndex index = model->index(QDir::currentPath());
    this->treeView->expand(index);
    this->treeView->scrollTo(index);
    this->treeView->resizeColumnToContents(0);
    //connect(this->btnRemove,SIGNAL(clicked()),this,SLOT(removeFile()));
    return 0;
    
}

void NewForm::removeFile()
{
    QModelIndex index = treeView->currentIndex();
    if(!index.isValid())
    {
        return;
    }
    bool OK;
    if(model->fileInfo(index).isDir())
    {
        return;
    }
    else
    {
        //OK = model->remove(index);
        fileName = model->filePath(index);

        //fileName = QDir::currentPath()+"/" + fileName;
    }
    //{
//        QMessageBox::information(this,tr("Remove"),
//                tr("Filed to move %1").arg(model->fileName(index)));
    //}
}
void NewForm::showMessage(QString s)
{
    if(s.toStdString() == "")
    {
        return;
    }
    if(s.toInt()>=100 || s.toInt()<=0)
    {
        QMessageBox::information(this, tr("提示"), tr("您输入的数字过大"));
    }
    else
    {
        return;
    }
}
void NewForm::showMsg(QString s)
{
     if(s.toStdString() == "")
    {
        return;
    }
    if(s.toInt()>=50 || s.toInt()<=0)
    {
        QMessageBox::information(this, tr("提示"), tr("请输入正确数字！"));
    }
    else
    {
        return;
    }
}
//点击进入第二个界面 并设定图片 
void NewForm::showSecondFormC()
{
    if(0//this->leHight->text().toStdString() == ""||this->leSpace->text().toStdString() == ""
            )
    {
        QMessageBox::information(this,tr("提示!"),tr("请填写完整参数！"));
        return;
    }
    
    //this->hide();
    this->reject();
    //在此添加图片
    IplImage *img;
    CvCapture *capture = cvCaptureFromAVI("/home/lynn/桌面/test01.avi");
    if(!capture){

    }
    //cvNamedWindow("tmp", 1);
    img = cvQueryFrame(capture);
	if (img)
	{
            secondForm *sForm = new secondForm(img, this);
            sForm->show();
            while(img){
                //cvShowImage("tmp", img);
                cvWaitKey(20);
                sForm->nextFrame(img);
                img = cvQueryFrame(capture);
            }
	}

    

}
void NewForm::showSecondFormR()
{
    if(0//this->leHight->text().toStdString() == ""||this->leSpace->text().toStdString() == ""
            )
    {
        QMessageBox::information(this,tr("提示!"),tr("请填写完整参数！"));
        return;
    }

    QModelIndex index = treeView->currentIndex();
    if(!index.isValid())
    {
        return;
    }
    bool OK;
    if(model->fileInfo(index).isDir())
    {
        return;
    }
    else
    {
        //OK = model->remove(index);
        fileName = model->filePath(index);

        //fileName = QDir::currentPath()+"/" + fileName;
    }

    
    if(fileName.toStdString() == "")
    {
        QMessageBox::information(this,tr("提示!"),tr("请选择播放文件！"));
                return;
    }
//  调试使用
//     QMessageBox::information(this,tr("Remove"),
//                tr("Filed to move %1").arg(fileName));
    this->hide();
    char path[200];
    sprintf(path,"%s", fileName.toStdString().c_str());
    StartAvi(path, this);
//    IplImage *img;
//    CvCapture *capture = cvCaptureFromAVI(fileName.toStdString().c_str());
//    if(!capture){
//
//    }
//    //cvNamedWindow("tmp", 1);
//    img = cvQueryFrame(capture);
//	if (img)
//	{
//
//            while(img){
//                //cvShowImage("tmp", img);
//                cvWaitKey(20);
//                sForm->nextFrame(img);
//                img = cvQueryFrame(capture);
//            }
//	}
//    secondForm *sForm = new secondForm(img);
//    sForm->show();
}